package org.microblog.service.impl;

import org.microblog.dao.BlogDao;
import org.microblog.dao.CollectionDao;
import org.microblog.dao.impl.BlogDaoImpl;
import org.microblog.dao.impl.CollectionDaoImpl;
import org.microblog.entity.Blog;
import org.microblog.entity.Collection;
import org.microblog.service.CollectionService;
import org.microblog.util.DatabaseUtil;
import org.microblog.util.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CollectionServiceImpl implements CollectionService {
    @Override
    public int getCollectionCountByBid(int bid) throws SQLException {
        Connection conn = null;
        try{
            conn  = DatabaseUtil.getConnection();
            return new CollectionDaoImpl(conn).getCollectionCountByBid(bid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public int addCollection(Collection collection) throws SQLException {
        Connection conn = null;
        int result;
        try {
            conn = DatabaseUtil.getConnection();
            conn.setAutoCommit(false);

            CollectionDao collectionDao = new CollectionDaoImpl(conn);
            if(collectionDao.findCollection(collection.getUid(),collection.getBid())==null){
                result = collectionDao.addCollection(collection);
            }else{
                result = -1;
            }

            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            DatabaseUtil.closeAll(conn, null, null);
        }
        return result;
    }

    @Override
    public Collection findCollection(int uid, int bid) throws SQLException {
        Connection conn = null;
        try{
            conn  = DatabaseUtil.getConnection();
            return new CollectionDaoImpl(conn).findCollection(uid,bid);
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }

    @Override
    public int deleteCollection(Collection collection) throws SQLException {
        Connection conn = null;
        int result;
        try {
            conn = DatabaseUtil.getConnection();
            conn.setAutoCommit(false);

            CollectionDao collectionDao = new CollectionDaoImpl(conn);
            if(collectionDao.findCollection(collection.getUid(),collection.getBid())!=null){
                result = collectionDao.deleteCollection(collection);
            }else{
                result = -1;
            }

            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            DatabaseUtil.closeAll(conn, null, null);
        }
        return result;
    }

    @Override
    public int deleteCollectionByBid(int bid) throws SQLException {
        Connection conn = null;
        int result;
        try {
            conn = DatabaseUtil.getConnection();
            conn.setAutoCommit(false);

            CollectionDao collectionDao = new CollectionDaoImpl(conn);
            result = collectionDao.deleteCollectionByBid(bid);

            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            if (conn != null)
                try {
                    conn.rollback();
                } catch (SQLException e1) {
                    e1.printStackTrace();
                }
            throw e;
        } finally {
            DatabaseUtil.closeAll(conn, null, null);
        }
        return result;
    }

    @Override
    public void getCollectionsByPage(int uid, String keyword, Page pageObj) throws SQLException {
        Connection conn = null;
        try{
            conn = DatabaseUtil.getConnection();
            CollectionDao collectionDao = new CollectionDaoImpl(conn);
            int totalCount = collectionDao.getCollectionCountByUidAndKeyword(uid,keyword);
            pageObj.setTotalCount(totalCount); // 设置总数量并计算总页数
            if (totalCount > 0) {
                // 对末页进行控制
                if (pageObj.getCurrPageNo() > pageObj.getTotalPageCount())
                    pageObj.setCurrPageNo(pageObj.getTotalPageCount());
                // 进行分页查询
                List<Blog> blogList = collectionDao.getCollectionsByPage(uid,keyword,pageObj.getCurrPageNo(),pageObj.getPageSize());
                pageObj.setBlogsList(blogList);
            } else {
                pageObj.setCurrPageNo(0);
                pageObj.setBlogsList(new ArrayList<Blog>());
            }
        }catch(SQLException e){
            e.printStackTrace();
            throw e;
        }finally{
            DatabaseUtil.closeAll(conn,null,null);
        }
    }
}
